<template>
  <div class="password-reset-container">
    <el-card class="reset-card" shadow="hover">
      <h2 class="reset-title">找回密码</h2>
      <el-form :model="form" :rules="rules" ref="resetForm" label-width="100px">
        <!-- 邮箱输入框 -->
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入注册邮箱"></el-input>
        </el-form-item>

        <!-- 验证码输入框 -->
        <el-form-item label="验证码" prop="code">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-input v-model="form.code" placeholder="请输入验证码"></el-input>
            </el-col>
            <el-col :span="12">
              <el-button
                :disabled="isCodeSending"
                @click="sendCode"
                type="primary"
              >
                {{ isCodeSending ? `${countdown}s` : "获取验证码" }}
              </el-button>
            </el-col>
          </el-row>
        </el-form-item>

        <!-- 密码输入框 -->
        <el-form-item label="新密码" prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入新密码"
            show-password
          ></el-input>
        </el-form-item>

        <!-- 确认密码输入框 -->
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            placeholder="请确认新密码"
            show-password
          ></el-input>
        </el-form-item>

        <!-- 提交按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submitForm">重置密码</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import { isEmail } from "@/utils/utils";

// 表单数据和校验规则
const form = reactive({
  email: "",
  code: "",
  password: "",
  confirmPassword: "",
});

const rules = {
  email: [
    { required: true, message: "请输入邮箱", trigger: "blur" },
    {
      type: "email",
      message: "请输入有效的邮箱地址",
      trigger: ["blur", "change"],
    },
  ],
  code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
  password: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    { min: 6, message: "密码长度不能少于6位", trigger: "blur" },
  ],
  confirmPassword: [
    { required: true, message: "请确认新密码", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (value !== form.password) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
};

// 验证码相关逻辑
const isCodeSending = ref(false);
const countdown = ref(60);

const sendCode = () => {
  if (!isEmail(form.email)) {
    ElMessage.warning("请先输入正确的邮箱地址");
    return;
  }

  isCodeSending.value = true;
  // 模拟发送验证码 API 调用
  setTimeout(() => {
    ElMessage.success("验证码已发送至您的邮箱");
    const interval = setInterval(() => {
      countdown.value -= 1;
      if (countdown.value <= 0) {
        clearInterval(interval);
        isCodeSending.value = false;
        countdown.value = 60;
      }
    }, 1000);
  }, 1000);
};

// 提交表单
const resetForm = ref(null);
const submitForm = () => {
  resetForm.value.validate((valid) => {
    if (valid) {
      ElMessage.success("密码重置成功");
      // 调用后端 API
    } else {
      ElMessage.error("表单验证失败");
    }
  });
};
</script>

<style scoped lang="scss">
.password-reset-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background:$background-color;
}
.reset-card {
  width: 400px;
  padding: 20px;
}

.reset-title {
  text-align: center; /* 标题居中 */
  margin-bottom: 20px; /* 增加下方间距 */
  font-size: 20px; /* 调整标题字体大小 */
  color: #333; /* 可选：设置字体颜色 */
}
</style>
